﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using NLog;

namespace Liwey.Common
{
    /**
     * 
     */
    public class LED
    {
        #region Native Methods

        //添加节目
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddProgram(int CardNum, Boolean bWaitToEnd, int iPlayTime);

        //添加单行文本区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddSingleText(int CardNum, ref User_SingleText pSingleText, int iProgramIndex);

        //添加文本区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddText(int CardNum, ref User_Text pText, int iProgramIndex);

        //添加时间区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddTime(int CardNum, ref User_DateTime pdateTime, int iProgramIndex);

        //添加图文区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddBmpZone(int CardNum, ref User_Bmp pBmp, int iProgramIndex);
        //指定图像句柄添加图片
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_AddBmp(int CardNum, int iBmpPartNum, IntPtr hBitmap, ref User_MoveSet pMoveSet, int iProgramIndex);
        //指定图像路径添加图片
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_AddBmpFile(int CardNum, int iBmpPartNum, string strFileName, ref User_MoveSet pMoveSet, int iProgramIndex);

        //添加RTF区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddRTF(int CardNum, ref User_RTF pRTF, int iProgramIndex);

        //添加计时区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddTimeCount(int CardNum, ref User_Timer pTimeCount, int iProgramIndex);

        //添加温度区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddTemperature(int CardNum, ref User_Temperature pTemperature, int iProgramIndex);

        //删除所有节目
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_DelAllProgram(int CardNum);

        //发送数据
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_SendToScreen(int CardNum);

        //校正板卡时间
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_AdjustTime(int CardNum);

        //开机
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_OpenScreen(int CardNum);

        //关机
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_CloseScreen(int CardNum);


        //实时建立连接
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_RealtimeConnect(int CardNum);

        //实时关闭连接
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_RealtimeDisConnect(int CardNum);

        //实时发送图片数据
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_RealtimeSendData(int CardNum, int x, int y, int iWidth, int iHeight, IntPtr hBitmap);


        //实时发送图片文件
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern Boolean User_RealtimeSendBmpData(int CardNum, int x, int y, int iWidth, int iHeight, string strFileName);

        [DllImport("gdi32.dll")]
        public static extern int SetTextCharacterExtra(IntPtr hdc, int charSpace);  
        #endregion

        protected static Logger log = LogManager.GetCurrentClassLogger();
        private int cardNum = 0;
        private int width = 128;
        private int height = 64;

        //format
        private string fontName = "宋体";
        private float fontSize = 8;
        private int charSpace = 1;
        private int lineSpace = 3;

        public LED(int cardNum)
        {
            if (cardNum <= 0)
            {
                throw new Exception("LED屏幕编号应大于0：" + cardNum);
            }

            this.cardNum = cardNum;

            bool ret = User_CloseScreen(this.cardNum);
            if (!ret)
            {
                throw new Exception("初始化LED屏幕" + cardNum + "失败");
            }

            //read width,height
            IniFile ini = new IniFile(".\\EQ2008_Dll_Set.ini");
            height = int.Parse(ini.ReadKey("地址：" + (cardNum -1), "ScreemHeight"));
            width = int.Parse(ini.ReadKey("地址：" + (cardNum - 1), "ScreemWidth"));

            fontName = ini.ReadKey("地址：" + (cardNum - 1), "FontName");
            fontSize = float.Parse(ini.ReadKey("地址：" + (cardNum - 1), "FontSize"));
            charSpace = int.Parse(ini.ReadKey("地址：" + (cardNum - 1), "charSpace"));
            lineSpace = int.Parse(ini.ReadKey("地址：" + (cardNum - 1), "lineSpace"));
        }

        public bool SendText(string text)
        {
            bool ret = User_RealtimeConnect(this.cardNum);
            if(ret){
                Bitmap bmp = createBmp(text);
                ret = User_RealtimeSendData(this.cardNum, 0, 0, this.width, this.height, bmp.GetHbitmap());
                if(ret)
                   return User_RealtimeDisConnect(this.cardNum);
            }
            return false;
        }

        private Bitmap createBmp(string str)
        {
            string[] lines = str.Split(new string[] { "\\n", "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

            Bitmap bmp = new Bitmap(this.width, this.height);
            Graphics g = Graphics.FromImage(bmp);
            SolidBrush sbrush = new SolidBrush(Color.Black);
            g.FillRectangle(sbrush, 0, 0, this.width, this.height);

            IntPtr hdc = g.GetHdc();
            SetTextCharacterExtra(hdc, charSpace); // 设置字符间距   
            g.ReleaseHdc(hdc); 
            
            Font font = new Font(this.fontName, this.fontSize);
            SolidBrush sbrush2 = new SolidBrush(Color.Red);
            StringFormat stringFormat = new StringFormat();
            stringFormat.LineAlignment = StringAlignment.Center;
            stringFormat.Alignment = StringAlignment.Center;
            int y = 0;
            foreach (string line in lines)
            {
                Rectangle rect = new Rectangle(0, y, this.width, font.Height + lineSpace);
                g.DrawString(line, font, sbrush2, rect, stringFormat);
                y = y + font.Height + lineSpace;
            }
            g.Dispose();
            return bmp;
        }

        public void DeleteAllPrograms()
        {
            User_DelAllProgram(this.cardNum);
        }
    }
}